layui.config({
	base : "js/"
}).use(['form','layer','jquery','layedit','laydate'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		layedit = layui.layedit,
		laydate = layui.laydate,
		$ = layui.jquery;

    var filedir = "drama/"; // 对应oss目录

	// 封面图上传是否达5张
	function checkImgUpload(){
		if(imgUrlArr.length < 1){
			top.layer.alert("剧照必须上传一张以上",{icon:0});
			return false;
		}
		return true;
	}


    //自定义验证规则
    form.verify({
        title: function(value){
            if(value.length < 5 || value.length > 30){
                return '标题至少得5个字啊！';
            }
        }, fname: function(value){
            if(value.length < 4){
                return '请输入至少4位的用户名';
            }
        }, contact: function(value){
            if(value.length < 4){
                return '内容请输入至少4个字符';
            }
        }
        ,phone: [/^1[3|4|5|7|8]\d{9}$/, '手机必须11位，只能是数字！']
        ,email: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
        // ,pass: [/(.+){6,12}$/, '密码必须6到12位']
        // ,content: function(value){
        //     layedit.sync(editIndex);
        // }
    });

	form.on("submit(addDrama)",function (data) {
		if(!checkImgUpload()) return false;
		var formData = $("#addDramaForm").serialize();
		$.ajax({
			cache: false,
			type: 'POST',
			url: "/drama/addDrama",
			dataType: "json",
			data: formData,
			async: false,
			success: function(data){
				if(data.result == "success"){
					top.layer.alert("保存成功",{icon:1});
					saveFlag = true;
				}else{
					top.layer.alert("保存异常,请重试",{icon:0});
				}
			},
			error: function(){
				top.layer.alert("发生异常,请重试",{icon:2});
			}
		})

		if(!saveFlag) return false;
	});

    var imgUrlArr = [];
    initCoverImgUpload();
    // 剧照图上传初始化
    function initCoverImgUpload() {
        $.Tupload.init({
            url: '/oss/ossImgUpload?filedir='+filedir,
            title: "剧照大小不能超过300kb<span style='color:red;'>(*第一张将被作为封面)</span>",
            fileNum: 5, // 上传文件数量
            divId: "T_upload", // div  id
            accept: "image/jpeg,image/x-png,image/jpg", // 上传文件的类型
            fileSize:300,     // 上传文件的大小 kb
            filedir:filedir, // 图片上传至oss对应的目录
            onSuccess: function(data, i) {
                console.log(i+">>>>>>>>"+JSON.stringify(data));
                imgUrlArr[i] = data["link"];
                $("#dramaUrl").val(imgUrlArr.join("@@@@"));
            },
            onDelete: function(i) {

            }
        });
    }

    var actorsName = [];
    var actorsId = [];
    $("#actors").click(function(){
            var index = layui.layer.open({
            title : "演员选择",
            type : 2,
            content : "../../page/person/actorOpenList.html",
            shadeClose: true,
            closeBtn: 1,
            maxmin: true, //开启最大化最小化按钮
            area: ['860px', '560px'],
            btn: ['确认'],
            yes: function(index, layero){
                var $checkbox = $("#actors_list tbody input[type=\"checkbox\"][name=\"checked\"]", layero.find("iframe")[0].contentWindow.document);
                var $checked = $("#actors_list tbody input[type=\"checkbox\"][name=\"checked\"]:checked", layero.find("iframe")[0].contentWindow.document);

                var objs = [];

                var obj = {};
                for(var j=0;j<$checked.length;j++){
                    obj = $($checked[j]).closest("td").next("td");
                    actorsName.push(obj.text());
                    actorsId.push(obj.attr("actorId"));
                }
                $("#actors").val(actorsName.join(","));
                $("#actorId").val(actorsId.join(","));

                layui.layer.closeAll();
            }
            // success : function(layer, index){
            //     setTimeout(function(){
            //         layui.layer.tips('点击此处返回剧集列表', '.layui-layer-setwin .layui-layer-close', {
            //             tips: 3
            //         });
            //     },500)
            // }
        })
    });

})
